/*
 * Author: JacksonCoder
 * FileName: HoresRace
 * Version: 1.0.0
 * Date: 2025/2/23
 */

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;

public class HorseRace {

    // 用于存储赛马成绩
    private static List<String> results = Collections.synchronizedList(new ArrayList<>());

    // 用于确保所有马同时开跑
    private static CountDownLatch startLatch = new CountDownLatch(1);

    // 用于确保所有马到达终点后再宣布成绩
    private static CyclicBarrier finishBarrier = new CyclicBarrier(10, new Runnable() {

        @Override
        public void run() {
            // 裁判宣布成绩
            System.out.println("Race finished! Announcing results: ");
            for (String result : results) {
                System.out.println(result);
            }
        }
    });

    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            new Thread(new Horse("Horse " + i)).start();
        }

        // 所有马就绪后开跑
        try {
            System.out.println("All horses ready. Race starts now!");
            startLatch.countDown(); // 马开跑
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static class Horse implements Runnable {
        private String name;

        public Horse(String name) {
            this.name = name;
        }

        @Override
        public void run() {
            try {
                // 马就绪，等待开跑
                startLatch.await();

                //马开始跑
                long raceTime = (long) (Math.random() * 5000); // 模拟跑的时间
                Thread.sleep(raceTime);

                // 马到终点
                results.add(name + " finished in " + raceTime + " ms");

                // 等待其他马到达终点
                finishBarrier.await();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
